package com.snake.springboot.authority.service.impl;

import com.snake.springboot.authority.api.model.AccountInfoModel;
import com.snake.springboot.authority.api.model.AuthSysAdminLoginModel;
import com.snake.springboot.authority.api.model.AuthSysAdminModel;
import com.snake.springboot.authority.api.service.*;
import org.snake.common.security.MD5Util;
import org.snake.common.security.jwt.JwtHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 类 {@code LoginSystemAdminServiceImpl}  <br> .
 * <p>系统管理员登录实现类
 * <p>创建时间：2025/01/14
 *
 * @author Huang.yj
 */
@Service
public class LoginSystemAdminServiceImpl implements LoginSystemAdminService {

    private AuthSysAdminService authSysAdminService;
    private VerifyImageService verifyImageService;
    private JwtHelper custJwtHelper;
    private AuthUsedService authUsedService;
    private VerifyTextCodeService verifyTextCodeService;

    @Autowired
    public LoginSystemAdminServiceImpl(AuthSysAdminService authSysAdminService, VerifyImageService verifyImageService,
                                       JwtHelper custJwtHelper, AuthUsedService authUsedService,
                                       VerifyTextCodeService verifyTextCodeService){
        this.authSysAdminService = authSysAdminService;
        this.verifyImageService = verifyImageService;
        this.custJwtHelper = custJwtHelper;
        this.authUsedService = authUsedService;
        this.verifyTextCodeService = verifyTextCodeService;
    }

    @Override
    public AuthSysAdminLoginModel loginByPassword(AuthSysAdminLoginModel model) throws Exception {
        //图片验证
        //verifyImageService.checkVerifyImage(model.getVerifyImageModel());
        //文字验证
        verifyTextCodeService.checkVerifyCode(model.getVerifyTextCodeModel());

        AuthSysAdminModel account = authSysAdminService.querySingleSystemAdminByAccount(model.getAccountData().getSysAdminAccount());
        if(account==null) throw new Exception("账号或者密码不正确");
        if(!account.getPassword().equals(MD5Util.encrypt(model.getAccountData().getPassword()))) throw new Exception("账号或者密码不正确");
        if(account.getEnable()==0) throw new Exception("账号已禁用");
        String token = custJwtHelper.sign(account.getSysAdminId(), 9, MD5Util.encrypt(account.getPassword()));
        model.setToken(token);
        //删除缓存数据
        AccountInfoModel accountInfoModel = new AccountInfoModel();
        accountInfoModel.setAccountId(account.getSysAdminId());
        authUsedService.delCache(accountInfoModel);

        AuthSysAdminLoginModel result = new AuthSysAdminLoginModel();
        result.setAccountData(account);
        result.setToken(token);
        return result;
    }

    @Override
    public AccountInfoModel getAccount(AccountInfoModel model) {
        AuthSysAdminModel sysAdminModel = authSysAdminService.querySingleSystemAdmin(model.getAccountId());
        model.setAccount(sysAdminModel.getAdminName());
        model.setType(9);
        model.setAccountId(sysAdminModel.getSysAdminId());
        model.setSecret(MD5Util.encrypt(sysAdminModel.getPassword()));
        return model;
    }
}
